package com.zw.admin.server.controller;


import com.zw.admin.server.dto.strategy.AddStrategyDto;
import com.zw.admin.server.dto.strategy.AddStrategyVo;
import com.zw.admin.server.dto.strategy.DealStrategyDto;
import com.zw.admin.server.dto.strategy.DealStrategyResultDto;
import com.zw.admin.server.model.Device;
import com.zw.admin.server.model.ResponseBean;
import com.zw.admin.server.model.Strategy;
import com.zw.admin.server.service.StrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "策略管理")
@RestController
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private StrategyService strategyService;


    @PostMapping("add")
    @ApiOperation("新增策略")
    public ResponseBean insertStrategy(@RequestBody AddStrategyDto strategyDto){
        strategyService.insertStrategy(strategyDto);
        return ResponseBean.ok();
    }

    @GetMapping("deviceList")
    @ApiOperation("未绑定策略的设备列表")
    public ResponseBean getDeviceList(@RequestParam(required = false) Integer spaceId){
        List<Device> resultList = strategyService.getDeviceList(spaceId);
        return ResponseBean.ok(resultList);
    }

    @GetMapping("queryStrategy")
    @ApiOperation("策略列表")
    public ResponseBean queryStrategy(){
        List<AddStrategyVo> list =  strategyService.queryStrategy();
        return ResponseBean.ok(list);
    }

    @DeleteMapping("delete/{strategyId}")
    @ApiOperation("删除策略")
    public ResponseBean deleteStrategy(@PathVariable("strategyId") Integer strategyId){
        strategyService.deleteStrategy(strategyId);
        return ResponseBean.ok();
    }


    @PutMapping("update")
    @ApiOperation("修改策略")
    public ResponseBean updateStrategy(@RequestBody AddStrategyDto strategyDto){
        if(strategyDto.getStrategyId() == null){
            return ResponseBean.error(100,"策略id不能为空");
        }
        strategyService.updateStrategy(strategyDto);
        return ResponseBean.ok();
    }

    @PostMapping("dealStrategyDevice")
    @ApiOperation("控制策略设备的一键开关")
    public ResponseBean dealStrategyDevice(@RequestBody DealStrategyDto dealStrategyDto){
        List<DealStrategyResultDto> list = strategyService.dealStrategyDevice(dealStrategyDto);
        return ResponseBean.ok(list);
    }
}
